﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ZK.MyControl
{
    public partial class UCLableBox : UserControl
    {

        public event EventHandler EditChanged; //设置值后触发事件
        delegate void Delegate_FucStr(string str);//委托

        public delegate void ValueChange(double  dvalue);//委托
        public event ValueChange ValueChangeHandler;
        public UCLableBox()
        {
            InitializeComponent();
            this.BorderStyle = BorderStyle.FixedSingle;
            textBox.Visible = false;
            label.Click += label_Click;
            textBox.KeyUp += textBox_KeyUp;
            textBox.KeyPress += textBox_KeyPress;
            textBox.LostFocus += textBox_LostFocus;
        }

        [Description("最大值")]
        public int MaxVal { get; set; } = 100;
        [Description("最小值")]
        public int MinVal { get; set; } = 0;
        public Label ULable {
            get
            {
                return  label;
            }
            set
            {
                label = value;
            }
        
        }


        public bool IsInt { get; set; } = true;
    
     

        public TextBoxEx UTextBox
        {
            get
            {
                return textBox;
            }
            set
            {
                textBox = value;
            }

        }

        private int lableValue;
        public int Value
        {
            get
            {
             
                  return lableValue;
              
            }

            set {

               
                    lableValue = value;

                    ULable.Text = lableValue.ToString();

                    if (ValueChangeHandler != null)
                    {
                        ValueChangeHandler(lableValue);
                    }
             
            }
        }

        /// <summary>
        /// 重写字体属性
        /// </summary>
        public override Font Font
        {
            get
            {
                return base.Font;
            }
            set
            {
                base.Font = value;

                label.Font = value;
                textBox.Font = new Font(value.FontFamily, value.Size+3);
                this.Height = TextRenderer.MeasureText("字体", textBox.Font).Height+8;
            }
        }

        /// <summary>
        /// 单击事件
        /// </summary>
        private void label_Click(object sender, EventArgs e)
        {
             
                textBox.Text = "";
                textBox.Visible = true;
                textBox.Focus();
            
        }

        /// <summary>
        /// 按键事件
        /// </summary>
        private void textBox_KeyUp(object sender,   KeyEventArgs e)
        {

            if (e.KeyCode == Keys.Escape)
            {
                textBox.Visible = false;
            }
            else if (e.KeyCode == Keys.Enter)
            {
                textBox.Visible = false;
                if (string.IsNullOrEmpty(textBox.Text))
                {
                    return;
                }

                if (int.TryParse(this.textBox.Text.Trim(), out int val))
                {
                    if (val > MaxVal)
                    {
                        Value = MaxVal;
                    }
                    else if (val < MinVal)
                    {
                        Value = MinVal;
                    }
                    else {
                        Value= val;
                    }
                }

        
                if (this.EditChanged != null)
                {
                    this.EditChanged(this, null);
                }
            }
        }

        /// <summary>
        /// 只允许输入数字和小数点
        /// </summary>
        private void textBox_KeyPress(object sender, KeyPressEventArgs e)
        {


            if (!char.IsNumber(e.KeyChar) && e.KeyChar != (char)8)
            {
                e.Handled = true;

            }

           
            //if (!char.IsNumber(e.KeyChar) && e.KeyChar != (char)8 && e.KeyChar != '.' && e.KeyChar != (char)45 && e.KeyChar != '-')
            //{
            //    e.Handled = true;

            //}

            //if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
            //{
            //    e.Handled = true;
            //}


        }

        public void LableShow(string str)
        {
            if (this.ULable.InvokeRequired)
            {
                Delegate_FucStr delegate_Fuc = new Delegate_FucStr(LableShow);

                this.ULable.Invoke(delegate_Fuc, new object[] { str });
            }
            else
            {
                this.textBox.Text = str;
                //this.ULable.Text = str;
                LblShow(str, this.ULable);
            }
        }

        private void LblShow(string label_str , Label label)
        {
            Graphics graphics = CreateGraphics();

            SizeF sizeF = graphics.MeasureString(label_str, label.Font);

            if (sizeF.Width >= label.Width - 20)
            {
                int det = (int)(sizeF.Width - label.Width + 20);

                int subindex = (label_str.Length * det / (label.Width - 10))+1;
                label.Text = label_str.Substring(0, label_str.Length - 1 - subindex) + "...";

            }
            else
            {
                label.Text = label_str;
            }

        }
        public void textBox_LostFocus(object sender, EventArgs e)
        {
            if (textBox.Visible) {
                textBox.Visible = false;
                if (string.IsNullOrEmpty(textBox.Text))
                {
                    return;
                }
                if (int.TryParse(this.textBox.Text.Trim(), out int val))
                {
                    if (val > MaxVal)
                    {
                        Value = MaxVal;
                    }
                    else if (val < MinVal)
                    {
                        Value = MinVal;
                    }
                    else
                    {
                        Value = val;
                    }
                }
                if (this.EditChanged != null)
                {
                    this.EditChanged(this, null);
                }
            }
            
        }

    }
}
